/*
 * Copyright 2011-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.platform.system.admin.action;

import java.util.List;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import me.ijleex.platform.framework.entity.Result;
import me.ijleex.platform.framework.page.Page;
import me.ijleex.platform.framework.spring.ControllerSupport;
import me.ijleex.platform.system.admin.entity.Admin;
import me.ijleex.platform.system.admin.entity.AdminRole;
import me.ijleex.platform.system.admin.service.IAdminRoleService;
import me.ijleex.platform.system.admin.service.IAdminService;

/**
 * 管理员 信息操作处理
 *
 * @author liym
 * @since 2019-03-14 11:03:53 By 'codegen'
 */
@RequestMapping("/system/admin")
@RestController
public class AdminAction extends ControllerSupport<Admin> {

    private final IAdminService service;
    private final IAdminRoleService adminRoleService;

    public AdminAction(IAdminService service, IAdminRoleService adminRoleService) {
        this.service = service;
        this.adminRoleService = adminRoleService;
    }

    /**
     * 新增 管理员
     */
    @PostMapping("/add")
    public Result add(Admin o) {
        int x = this.service.insertAdmin(o);
        return Result.of(x);
    }

    /**
     * 删除 管理员
     */
    @PostMapping("/remove")
    public Result remove(String ids) {
        int x = this.service.deleteAdminByIds(ids);
        return Result.of(x);
    }

    /**
     * 修改 管理员
     */
    @PostMapping("/update")
    public Result update(Admin o) {
        int x = this.service.updateAdmin(o);
        return Result.of(x);
    }

    /**
     * 查询 管理员列表
     */
    @GetMapping("/list")
    public Page<Admin> getAdminList(HttpServletRequest request, Admin o) {
        this.setPageParams(request, o);
        List<Admin> list = this.service.getAdminList(o);
        return this.wrapPageData(list, o);
    }

    /**
     * 查询 管理员信息
     *
     * @param request 请求
     * @param id ID
     * @return 管理员信息
     * @since 2019-03-25 11:17
     */
    // http://localhost:8082/ij-platform/system/admin/getById?id=577054734542819328
    @GetMapping("/getById")
    public Result getAdminById(HttpServletRequest request, Long id) {
        Admin admin = this.service.getAdminById(id);
        return admin == null ? Result.error("管理员不存在：id=" + id) : Result.ok(admin);
    }

    // ===

    /**
     * 得到已授权给指定管理员的角色的 id 列表
     *
     * @param request 请求
     * @param adminId 管理员 ID
     * @return 角色 ID 列表
     */
    // http://localhost:8082/ij-platform/system/admin/roleIds?adminId=577054734542819328
    @GetMapping("/roleIds")
    public List<Long> getRoleIdByAdminId(HttpServletRequest request, Long adminId) {
        return this.adminRoleService.getRoleIdByAdminId(adminId);
    }

    /**
     * 管理员授权（建立管理员与角色的关联关系）
     *
     * @param request 请求
     * @param object 管理员、角色关系信息，必须包含 adminId、roleIds 等
     * @return 返回信息
     */
    // curl -d "adminId=577054734542819328&roleIds=376597004234641408" http://localhost:8082/ij-platform/system/admin/grant
    @PostMapping("/grant")
    public Result grant(HttpServletRequest request, AdminRole object) {
        object.setCreator("me");
        return this.adminRoleService.grant(object);
    }

}
